package com.jweb.framework.ws.soap.axiom;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.transform.Result;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFaultDetail;
import org.springframework.ws.soap.SoapFaultDetail;
import org.springframework.ws.soap.SoapFaultDetailElement;
import org.springframework.xml.transform.StaxResult;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
class AxiomSoapFaultDetail extends AxiomSoapElement implements SoapFaultDetail {
	
	public AxiomSoapFaultDetail(SOAPFaultDetail axiomFaultDetail , SOAPFactory axiomFactory){
		super(axiomFaultDetail, axiomFactory);
	}
	
	public SoapFaultDetailElement addFaultDetailElement(QName name) {
		try {
			OMElement element = getAxiomFactory().createOMElement(name, getAxiomFaultDetail());
			return new AxiomSoapFaultDetailElement(element , getAxiomFactory());
		} catch (OMException e) {
			throw new AxiomSoapFaultException(e);
		}
	}
	
	
	public Iterator<SoapFaultDetailElement> getDetailEntries(){
		return new AxiomSoapFaultDetailElementIterator(getAxiomFaultDetail().getChildElements());
	}
	
	public Result getResult(){
		return new StaxResult(new DXMLStreamWriter(getAxiomFaultDetail(), getAxiomFactory()));
	}
	
	protected SOAPFaultDetail getAxiomFaultDetail(){
		return (SOAPFaultDetail) getAxiomElement();
	}
	
	private class AxiomSoapFaultDetailElementIterator implements Iterator<SoapFaultDetailElement> {
		
		private final Iterator<OMElement> axiomIterator ;
		
		private AxiomSoapFaultDetailElementIterator(Iterator<OMElement> axiomIterator) {
			this.axiomIterator = axiomIterator ;
		}
		
		public boolean hasNext(){
			return axiomIterator.hasNext();
		}
		
		public SoapFaultDetailElement next(){
			try {
				OMElement axiomElement = axiomIterator.next();
				return new AxiomSoapFaultDetailElement(axiomElement , getAxiomFactory());
			} catch (OMException e) {
				throw new AxiomSoapFaultException(e);
			}
		}
		
		public void remove(){
			axiomIterator.remove();
		}
		
	}
	
}
